<style scoped lang="scss">
@import url('index.scss');
</style>
<template>
  <div class="app-container">

    <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="企业名称" prop="mname">
              <el-input v-model="queryParams.mname" placeholder="请输入企业名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="常用简称" prop="shortname">
              <el-input v-model="queryParams.shortname" placeholder="请输入常用简称" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="法定代表" prop="legaluser">
              <el-input v-model="queryParams.legaluser" placeholder="请输入法定代表人" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="联系人" prop="contactuser">
              <el-input v-model="queryParams.contactuser" placeholder="请输入联系人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactphone">
              <el-input v-model="queryParams.contactphone" placeholder="请输入联系电话" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="信用等级" prop="creditlevel">
              <el-input v-model="queryParams.creditlevel" placeholder="请输入厂家信用等级" clearable
                @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item>
              <el-button class="newButton" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:manufacturer:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:manufacturer:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete"
            v-hasPermi="['system:manufacturer:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:manufacturer:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
      </el-row>
    </div>





    <el-table v-loading="loading" :data="manufacturerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" fixed align="center" />
      <el-table-column label="ID" width="55" fixed align="center" prop="mid" />
      <el-table-column label="企业名称" min-width="220" fixed align="center" prop="mname" />
      <el-table-column label="常用简称" width="120" fixed align="center" prop="shortname" />
      <el-table-column label="社会信用代码" min-width="180" align="center" prop="uniformcode" />
      <el-table-column label="法定代表人" width="120" fixed align="center" prop="legaluser" />
      <el-table-column label="注册地址" min-width="220" align="center" prop="regaddress" />
      <el-table-column label="生产地址" min-width="220" align="center" prop="proaddress" />
      <el-table-column label="联系人" width="120" fixed align="center" prop="contactuser" />
      <el-table-column label="联系电话" width="120" fixed align="center" prop="contactphone" />
      <el-table-column label="企业营业执照" width="120"  align="center" prop="cardfile" />
      <el-table-column label="厂家信用等级" width="120" align="center" prop="creditlevel" />
      <el-table-column label="药品生产许可证编号" width="180" align="center" prop="drugprono" />
      <el-table-column label="药品生产许可证" width="120" align="center" prop="drugprofile" />
      <el-table-column label="发证日期" width="120" align="center" prop="drugprotime" />
      <el-table-column label="证件有效期" width="120" align="center" prop="drugproendtime" />
      <el-table-column label="辐射安全许可证编号" width="180" align="center" prop="radiationLicenseNo" />
      <el-table-column label="发证日期" width="120" align="center" prop="radiationLicenseStart" />
      <el-table-column label="截止日期" width="120" align="center" prop="radiationLicenseEnd" />
      <el-table-column label="辐射安全许可证" width="128" align="center" prop="radiationLicenseFile" />
      <el-table-column label="状态" width="80" align="center" prop="status" >
        <template #default="scope">
          <el-tag :type="scope.row.status == '1' ? 'success' : 'danger'">{{ scope.row.status == '1' ? '正常' : '停用' }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" width="120" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:manufacturer:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:manufacturer:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改生产厂家对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">

      <div class="custom-style">
        <el-segmented v-model="currentTab" :options="optionsTab" />
      </div>

      <el-form ref="manufacturerRef" :model="form" :rules="rules" label-width="120px">

        <div class="tab-content" v-show="currentTab == 'tab1'">
          <el-form-item label="企业营业执照" prop="cardfile">
            <!-- <file-upload v-model="form.cardfile" /> -->
            <image-upload v-model="form.cardfile" />
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="企业名称" prop="mname">
                <el-input v-model="form.mname" placeholder="请输入企业名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="常用简称" prop="shortname">
                <el-input v-model="form.shortname" placeholder="请输入常用简称" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="社会信用代码" prop="uniformcode">
                <el-input v-model="form.uniformcode" placeholder="请输入社会信用代码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="法定代表人" prop="legaluser">
                <el-input v-model="form.legaluser" placeholder="请输入法定代表人" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="注册地址" prop="regaddress">
            <el-input v-model="form.regaddress" placeholder="请输入注册地址" />
          </el-form-item>
          <el-form-item label="生产地址" prop="proaddress">
            <el-input v-model="form.proaddress" placeholder="请输入生产地址" />
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="联系人" prop="contactuser">
                <el-input v-model="form.contactuser" placeholder="请输入联系人" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="contactphone">
                <el-input v-model="form.contactphone" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="厂家信用等级" prop="creditlevel">
                <el-input v-model="form.creditlevel" placeholder="请输入厂家信用等级" />
              </el-form-item>
            </el-col>
            <el-col :span="12">

            </el-col>
          </el-row>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in ha_normal_status" :key="dict.value" :value="dict.value" :label="dict.value">{{
                dict.label }}</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="6" />
          </el-form-item>
        </div>

        <div class="tab-content" v-show="currentTab == 'tab2'">
          <el-form-item label="药品生产许可证" prop="drugprofile">
            <!-- <file-upload v-model="form.drugprofile" /> -->
            <image-upload v-model="form.drugprofile" />
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="生产许可证编号" prop="drugprono">
                <el-input v-model="form.drugprono" placeholder="请输入药品生产许可证编号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发证日期" prop="drugprotime">
                <el-input v-model="form.drugprotime" placeholder="请输入发证日期" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="证件有效期" prop="drugproendtime">
                <el-input v-model="form.drugproendtime" placeholder="请输入证件有效期" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
            </el-col>
          </el-row>
        </div>
        <div class="tab-content" v-show="currentTab == 'tab3'">
          <el-form-item label="辐射安全许可证" prop="radiationLicenseFile">
            <!-- <file-upload v-model="form.radiationLicenseFile" /> -->
            <image-upload v-model="form.radiationLicenseFile" />
          </el-form-item>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="许可证编号" prop="radiationLicenseNo">
                <el-input v-model="form.radiationLicenseNo" placeholder="请输入辐射安全许可证编号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="发证日期" prop="radiationLicenseStart">
                <el-input v-model="form.radiationLicenseStart" placeholder="请输入发证日期" />
              </el-form-item>
            </el-col>
            <el-col :span="12">

              <el-form-item label="截止日期" prop="radiationLicenseEnd">
                <el-input v-model="form.radiationLicenseEnd" placeholder="请输入截止日期" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Manufacturer">
import { listManufacturer, getManufacturer, delManufacturer, addManufacturer, updateManufacturer } from "@/api/system/manufacturer"

const { proxy } = getCurrentInstance()
const { ha_normal_status } = proxy.useDict('ha_normal_status'); // 使用字典
const manufacturerList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    mname: null,
    shortname: null,
    uniformcode: null,
    legaluser: null,
    regaddress: null,
    proaddress: null,
    contactuser: null,
    contactphone: null,
    cardfile: null,
    creditlevel: null,
    drugprono: null,
    drugprofile: null,
    drugprotime: null,
    drugproendtime: null,
    radiationLicenseNo: null,
    radiationLicenseStart: null,
    radiationLicenseEnd: null,
    radiationLicenseFile: null,
    status: null,
  },
  rules: {
    mname: [
      { required: true, message: '请输入企业名称', trigger: 'blur' }
    ],
    drugprono: [
      { required: true, message: '请输入生产许可证编号', trigger: 'blur' }
    ],
    radiationLicenseNo: [
      { required: true, message: '请输入辐射安全许可证编号', trigger: 'blur' }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)
const currentTab = ref('tab1') // 当前选中的选项卡
const optionsTab = ref([
  {
    label: '基本信息',
    value: 'tab1'
  },
  {
    label: '生产许可证',
    value: 'tab2'
  },
  {
    label: '辐射安全许可证',
    value: 'tab3'
  }
])
/** 查询生产厂家列表 */
function getList() {
  loading.value = true
  listManufacturer(queryParams.value).then(response => {
    manufacturerList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    mid: null,
    mname: null,
    shortname: null,
    uniformcode: null,
    legaluser: null,
    regaddress: null,
    proaddress: null,
    contactuser: null,
    contactphone: null,
    cardfile: null,
    creditlevel: null,
    drugprono: null,
    drugprofile: null,
    drugprotime: null,
    drugproendtime: null,
    radiationLicenseNo: null,
    radiationLicenseStart: null,
    radiationLicenseEnd: null,
    radiationLicenseFile: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    status: '1',
    remark: null
  }
  proxy.resetForm("manufacturerRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.mid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加生产厂家"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _mid = row.mid || ids.value
  getManufacturer(_mid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改生产厂家"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["manufacturerRef"].validate(valid => {
    if (valid) {
      if (form.value.mid != null) {
        updateManufacturer(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addManufacturer(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _mids = row.mid || ids.value
  proxy.$modal.confirm('是否确认删除生产厂家编号为"' + _mids + '"的数据项？').then(function() {
    return delManufacturer(_mids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/manufacturer/export', {
    ...queryParams.value
  }, `manufacturer_${new Date().getTime()}.xlsx`)
}

getList()
</script>
